package org.neptune.service.service.impl;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.neptune.api.request.BaseRequest;
import org.neptune.common.constant.NeptuneConstant;
import org.neptune.common.response.LayuiTableResponse;
import org.neptune.service.elasticsearch.EsLogRepository;
import org.neptune.service.service.EsLogService;
import org.neptune.service.util.EsLogBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

/**
 * @author lwl
 * @date 2019/12/3 17:00
 */
@Service
public class EsLogServiceImpl extends BaseEsSearchServiceImpl<EsLogBean> implements EsLogService {

	private static final Logger logger = LoggerFactory.getLogger(EsLogServiceImpl.class);

	@Autowired
	private ElasticsearchTemplate elasticsearchTemplate;

	@Autowired
	private EsLogRepository esLogRepository;

	@Override
	public void addEsLogIndex() {
		elasticsearchTemplate.createIndex(EsLogBean.class);
	}

	@Override
	public void deleteEsLogIndex() {
		elasticsearchTemplate.deleteIndex(EsLogBean.class);
	}

	@Override
	public LayuiTableResponse<Map<String, Object>> queryByPage(BaseRequest baseRequest) {
		String keywords = "";
		if (StringUtils.isNotBlank(baseRequest.getKeywords())){
			keywords = baseRequest.getKeywords();
		}
		LayuiTableResponse<Map<String, Object>> result = this.queryHitByPage(baseRequest.getPage(),
				baseRequest.getLimit(), keywords, NeptuneConstant.ESLOGBEAN_INDEX_NAME, "content");

		return result;
	}

	@Override
	public void addEsLog(EsLogBean esLogBean) {
		try{
			esLogRepository.save(esLogBean);
		} catch (Exception e){
			logger.info("esLogBean add error : " + esLogBean.getTitle(), e);
		}
	}

	@Override
	public void addEsLogs(EsLogBean... args) {

	}

}
